﻿using OF.Lib.DataAccess.Entity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace OF.Lib.DataAccess.Core
{
    public static class SOConfigHelper
    {
        static Dictionary<string, SingleObject> m_SODic = null;

        private static object _obj = new object();
        public static SingleObject GetSingleObjectConnKey(string objectName)
        {
            if (m_SODic == null)
            {
                lock (_obj)
                {
                    string filePath = Path.Combine(GlobalDefine.ConfigurationPath, "SingleObjectDB.config");
                    if (File.Exists(filePath))
                    {


                        m_SODic = new Dictionary<string, SingleObject>();
                        SingleObjectConfig soConfig = XMLHelper.LoadFromXml<SingleObjectConfig>(filePath);
                        if (soConfig.SingleObjectList != null && soConfig.SingleObjectList.Count > 0)
                        {
                            foreach (SingleObject so in soConfig.SingleObjectList)
                            {
                                if (!m_SODic.ContainsKey(so.Name.Trim().ToUpper()))
                                {
                                    m_SODic.Add(so.Name.Trim().ToUpper(), so);
                                }
                            }
                        }
                    }
                    else
                    {
                        throw new Exception(string.Format("Not found SingleObjectDB.config file in \"{0}\"", filePath));
                    }
                }
            }
            if (!m_SODic.ContainsKey(objectName.Trim().ToUpper()))
            {
                throw new Exception(string.Format("Not found Single Object \"{0}\" configuration in SingleObjectDB.config.", objectName));
            }
            return m_SODic[objectName.Trim().ToUpper()];

        }
    }
}
